package com.example.litterpainterdemo.draw.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.example.litterpainterdemo.R
import com.example.litterpainterdemo.utils.dp2px
import com.example.litterpainterdemo.utils.sp2px


class ProgressView(context: Context,attrs:AttributeSet?):View(context,attrs){
    var viewModel: DrawViewModel? = null
    private var min = 0
    private var max = 0
    private var currentProgress = 0f
    private var mProgressWidth = dp2px(100) //进度条宽度
    private var mProgressHeight = dp2px(8)  //进度条高度
    private var mCircle = dp2px(10) //圆的半径
    private var mCorner = mProgressHeight/2 //圆角半径
    private var backgroundColor = resources.getColor(R.color.light_dark,null)
    private var progressColor = resources.getColor(R.color.main_red,null)
    private var textColor = Color.parseColor("#E5E5E5")
    private var mTextSize = context.sp2px(11f)
    //文本画笔
    private var mTextPaint = Paint().apply {
        textSize = mTextSize
        color = textColor
    }
    //进度条画笔
    private var mProgressPaint = Paint().apply {
        style = Paint.Style.FILL
    }

    init {
        //解析属性
        parseAttrs(attrs)
    }

    //解析属性
    private fun parseAttrs(attrs: AttributeSet?){
        context.obtainStyledAttributes(attrs, R.styleable.ProgressView).apply {
            min = getInteger(R.styleable.ProgressView_min,1)
            max = getInteger(R.styleable.ProgressView_max,30)
            currentProgress = getFloat(R.styleable.ProgressView_current_progress,10f)
            recycle()
        }
    }

    //测量
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        var mWidth = MeasureSpec.getSize(widthMeasureSpec)
        if(MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY){
            //需要自己决定
            mWidth = mProgressWidth.toInt()
        }

        var mHeight = MeasureSpec.getSize(heightMeasureSpec)
        if(MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY){
            //需要自己决定
            mHeight = (mCircle*2).toInt()
        }
        setMeasuredDimension(mWidth,mHeight)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mCorner = height/2f
        mProgressWidth = width.toFloat()
        mCircle = height/2f


        viewModel?.setLineSize(currentProgress.toInt())
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //绘制背景进度条
        drawBackgroundProgress(canvas)
        //绘制动态的进度条
        drawDynamicProgress(canvas)
        //绘制圆点
        drawCircleProgress(canvas)
        //绘制文本
        drawTextProgress(canvas)

    }

    //绘制背景进度条
    private fun drawBackgroundProgress(canvas: Canvas?){
        mProgressPaint.color = backgroundColor
        val left = 0f
        val right = mProgressWidth
        val top = (height-mProgressHeight)/2
        val bottom = top + mProgressHeight
        canvas?.drawRoundRect(left,top,right,bottom,mCorner,mCorner,mProgressPaint)
    }

    //绘制动态的进度条
    private fun drawDynamicProgress(canvas: Canvas?){
        mProgressPaint.color = progressColor
        val left = 0f
        val right = currentProgress/ max *mProgressWidth
        val top = (height-mProgressHeight)/2
        val bottom = top + mProgressHeight
        canvas?.drawRoundRect(left,top,right,bottom,mCorner,mCorner,mProgressPaint)
    }

    var cx = 0f
    //绘制圆点
    private fun drawCircleProgress(canvas: Canvas?){
        mProgressPaint.color = progressColor
        cx = currentProgress / max *mProgressWidth-mCircle
        if(cx - mCircle <= 0f){
            cx = mCircle
        }
        var cy = height/2f
        canvas?.drawCircle(cx,cy,mCircle,mProgressPaint)
    }

    //绘制文本
    private fun drawTextProgress(canvas: Canvas?){
        val text = "${currentProgress.toInt()}"
        val textWidth = mTextPaint.measureText(text)
        var tx = cx - textWidth/2
        var ty = 0f
        mTextPaint.fontMetrics.apply {
            ty = (height - this.top - this.bottom)/2
        }
        canvas?.drawText(text,tx,ty,mTextPaint)
    }


    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when(event?.action){
            MotionEvent.ACTION_DOWN -> {changeProgress(event.x)}
            MotionEvent.ACTION_MOVE -> {changeProgress(event.x)}
            MotionEvent.ACTION_UP -> {viewModel?.setLineSize(currentProgress.toInt())}
        }
        return true
    }

    //改变进度
    private fun changeProgress(mx:Float){
        currentProgress = mx/width.toFloat()*max.toFloat()
        if(currentProgress.toInt() <= 0){
            currentProgress = 1f
        }else if(currentProgress.toInt() >= max){
            currentProgress = max.toFloat()
        }

        invalidate()
    }


}